package fanshe4;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;

public class TextUser {
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        Class<?> userClass = Class.forName("fanshe4.User");
        //通过无参构造实例化对象
        Object o = userClass.newInstance();
        System.out.println("o="+o);

        Constructor<?> constructor = userClass.getConstructor(String.class);
        Object instance = constructor.newInstance("小明");
        System.out.println("instance = " + instance);

        Constructor<?> declaredConstructor = userClass.getDeclaredConstructor(int.class, String.class, String.class);
        declaredConstructor.setAccessible(true);//爆破，允许访问私有的
        Object o1 = declaredConstructor.newInstance(100, "bb", "足球");
        System.out.println("o1="+o1);

        System.out.println("--------------");
        Field[] fields = userClass.getFields();
        for (Field field : fields) {
            System.out.println(field.getName());
        }

        System.out.println("------------------------");
        Field[] fields1 = userClass.getDeclaredFields();
        for (Field field : fields1) {
            System.out.println(field.getName());
        }


    }
}
